package cn.fresh.xuanyuan.module.infra.service.column;

import cn.fresh.xuanyuan.framework.common.exception.ErrorCode;
import cn.fresh.xuanyuan.framework.common.exception.ServiceException;
import cn.fresh.xuanyuan.framework.common.pojo.PageParam;
import cn.fresh.xuanyuan.framework.common.pojo.PageResult;
import cn.fresh.xuanyuan.module.infra.dal.dataobject.column.MallColumnDO;
import cn.fresh.xuanyuan.module.infra.dal.dataobject.column.MallColumnProductDO;
import cn.fresh.xuanyuan.module.infra.dal.mysql.column.MallColumnMapper;
import cn.fresh.xuanyuan.module.infra.dal.mysql.column.MallColumnProductMapper;
import cn.fresh.xuanyuan.module.infra.dto.MallColumnProductDto;
import cn.fresh.xuanyuan.module.product.api.spu.ProductSpuApi;
import cn.fresh.xuanyuan.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Validated
@Slf4j
public class MallColumnProductServiceImpl implements MallColumnProductService {

    @Autowired
    private MallColumnProductMapper mallColumnProductMapper;

    @Autowired
    private MallColumnMapper mallColumnMapper;

    @Resource
    private ProductSpuApi productSpuApi;

    @Override
    public PageResult<MallColumnProductDO> getPageList(Integer columnId, String name, PageParam pageParam) {
        return mallColumnProductMapper.selectPage(columnId, name, pageParam);
    }

    @Override
    @Transactional
    public void addColumnProduct(MallColumnProductDto mallColumnProduct) {
        MallColumnDO mallColumnDO = mallColumnMapper.selectById(mallColumnProduct.getColumnId());
        if(mallColumnDO == null) {
            throw new ServiceException(new ErrorCode(400, "栏目不存在"));
        }
        ProductSpuRespDTO productSpuRespDTO = productSpuApi.getSpu(mallColumnProduct.getProductId()).getCheckedData();
        if(productSpuRespDTO == null) {
            throw new ServiceException(new ErrorCode(400, "商品不存在"));
        }
        LambdaQueryWrapper<MallColumnProductDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallColumnProductDO::getColumnId, mallColumnProduct.getColumnId());
        queryWrapper.eq(MallColumnProductDO::getProductId, mallColumnProduct.getProductId());
        queryWrapper.eq(MallColumnProductDO::getDeleted,false);
        MallColumnProductDO mallColumnProductDO = mallColumnProductMapper.selectOne(queryWrapper);
        if(mallColumnProductDO != null) {
            throw new ServiceException(new ErrorCode(400, "该商品已存在"));
        }
        MallColumnProductDO columnProduct = new MallColumnProductDO();
        BeanUtils.copyProperties(mallColumnProduct, columnProduct);
        columnProduct.setProductName(productSpuRespDTO.getName());
        columnProduct.setProductImage(productSpuRespDTO.getPicUrl());
        BigDecimal productPrice = new BigDecimal(productSpuRespDTO.getPrice());
        columnProduct.setSalePrice(productPrice);
        columnProduct.setPayPrice(productPrice.divide(new BigDecimal(100)));
        columnProduct.setCreateTime(LocalDateTime.now());
        mallColumnProductMapper.insert(columnProduct);
    }

    @Override
    @Transactional
    public void updateColumnProduct(MallColumnProductDto mallColumnProduct) {
        LambdaQueryWrapper<MallColumnProductDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallColumnProductDO::getColumnId, mallColumnProduct.getColumnId());
        queryWrapper.eq(MallColumnProductDO::getProductId, mallColumnProduct.getProductId());
        queryWrapper.eq(MallColumnProductDO::getDeleted,false);
        MallColumnProductDO mallColumnProductDO = mallColumnProductMapper.selectOne(queryWrapper);
        if(mallColumnProductDO == null) {
            throw new ServiceException(new ErrorCode(400, "该商品不存在"));
        }
        MallColumnProductDO columnProduct = new MallColumnProductDO();
        BeanUtils.copyProperties(mallColumnProduct, columnProduct);
        columnProduct.setUpdateTime(LocalDateTime.now());
        mallColumnProductMapper.updateById(columnProduct);
    }

    @Override
    public void deleteColumnProduct(Long id) {
        MallColumnProductDO columnProduct = mallColumnProductMapper.selectById(id);
        if(columnProduct != null) {
            mallColumnProductMapper.deleteById(id);
        }
    }

    @Override
    public List<MallColumnProductDO> getListBySpuId(Long spuId) {

        List<MallColumnProductDO> list = mallColumnProductMapper.getListBySpuId(spuId);

        return list;
    }

    @Override
    public void updateBatch(List<MallColumnProductDO> list) {
        if(CollectionUtil.isNotEmpty(list)) {
            mallColumnProductMapper.updateBatch(list);
        }
    }
}
